package com.ujiuye.dao;

import com.ujiuye.entity.Student;
import com.ujiuye.utils.PageUtils;

import java.util.List;

public class StudentDao extends BaseDao<Student> {
    // 查询所有学生信息
    public List<Student> queryStudents(){
        String sql="select * from stu";
        return queryMore(sql,Student.class);
    }
    // 根据id删除学生信息
    public int delStuById(String id){
        String sql = "delete from stu where sid = ?";
        return update(sql,id);
    }
    // 添加学生信息
    public int addStu(Student s){
        String sql = "insert into stu(sname,age,tel,sbir,sex,spic)values(?,?,?,?,?,?)";
        Object[] params={s.getSname(),s.getAge(),s.getTel(),s.getSbir(),s.getsex(),s.getSpic()};
        return update(sql,params);
    }
    // 根据id查询学生信息
    public Student queryStuById(String id){
        String sql="select * from stu where sid = ?";
        return queryOne(sql,Student.class,id);
    }
    // 修改学生信息
    public int updateStu(Student s){
        String sql = "update stu set sname=?,age=?,tel=?,sbir=?,sex=?,spic=? where sid = ?";
        Object[] params={s.getSname(),s.getAge(),s.getTel(),s.getSbir(),s.getsex(),s.getSpic(),s.getSid()};
        return update(sql,params);
    }
    // 分页查询
    public List<Student> queryStuPage(PageUtils p){
        String sql = "select * from stu limit ?,?";
        return queryMore(sql,Student.class,p.getStartIndex(),p.getPageSize());
    }
    // 查询总记录数
    public int queryNum(){
        String sql = "select count(*) from stu";
        return queryScar(sql).intValue();
    }
    // 模糊查询
    public List<Student> selStu(String name,String sex,PageUtils pu){
        StringBuffer sql =new StringBuffer("select * from stu where 1=1");
        if (name!=null&&"".equals(name)){
            sql.append(" and sname like '%"+name+"%' ");
        }
        if ("0".equals(sex)||"1".equals(sex)){
            sql.append(" and sex = "+sex);
        }
        sql.append(" limit ?,? ");
        return queryMore(sql.toString(),Student.class,pu.getStartIndex(),pu.getPageSize());
    }

    // 模糊查询记录统计
        public int selStuNum(String name,String sex){
        StringBuffer sql =new StringBuffer("select count(*) from stu where 1=1");
        if (name!=null&&"".equals(name)){
            sql.append(" and sname like '%"+name+"%' ");
        }
        if ("0".equals(sex)||"1".equals(sex)){
            sql.append(" and sex = "+sex);
        }
        return queryScar(sql.toString()).intValue();
    }
}
